import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import org.rsbot.event.events.MessageEvent;
import org.rsbot.event.listeners.MessageListener;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Bank;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Skills;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"Bloddyharry"}, name="Master Farmer Stealer", keywords={"Thieving"}, version=2.4D, description="Made by BloddyHarry. Settings in GUI.")
public class BloddyMasterFarmerStealer extends Script
  implements PaintListener, MessageListener
{
  final ScriptManifest properties;
  BufferedImage normal;
  BufferedImage clicked;
  public int farmerID;
  public int bBoothID;
  public int addFail;
  private boolean doDropSeeds;
  public boolean showInventory;
  private boolean withdraw10;
  private boolean highLevelMode;
  private boolean canSay;
  public boolean logOut;
  public boolean failed;
  public int wantedHours;
  public int wantedMinutes;
  public int wantedSeconds;
  public int wantedLevel;
  public int gainedLvls;
  public boolean finishAt;
  public boolean finishWhenLevel;
  private int startXP;
  public int[] seedID;
  public int[] junkSeedID;
  private int FOODID;
  private int ANIMATIONID;
  RSTile[] farmerToBank;
  RSTile farmerTile;
  RSTile bankTile;
  String status;
  private int HP;
  private int startLvl;
  public int foodEated;
  public int pickpockets;
  public int failPickpocket;
  public int somethingID;
  public long startTime;
  public int[] pickpocketing;
  private final BloddyMasterFarmerStealer.cameraHeight camHeight;
  private final BloddyMasterFarmerStealer.cameraRotate camRotate;
  private boolean guiWait;
  private boolean guiExit;
  BloddyMasterFarmerStealer.BloddyMasterFarmerGUI gui;

  public BloddyMasterFarmerStealer()
  {
    this.properties = ((ScriptManifest)getClass().getAnnotation(ScriptManifest.class));

    this.normal = null;
    this.clicked = null;
    this.farmerID = 2234;
    this.bBoothID = 2213;
    this.addFail = 0;
    this.doDropSeeds = true;
    this.showInventory = false;
    this.withdraw10 = false;
    this.highLevelMode = false;
    this.canSay = true;
    this.logOut = false; this.failed = false;

    this.startXP = 0;
    this.seedID = new int[] { 5304, 5296, 5300, 5295, 5303, 5302, 5100, 5323, 5299, 5301, 5298, 5320, 5321, 1161, 1965, 1969, 1967, 1895, 1893, 1891, 1971, 4293, 2142, 4291, 2140, 3228, 9980, 7223, 6297, 6293, 6295, 6299, 7521, 9988, 7228, 2878, 7568, 2343, 1861, 13433, 315, 325, 319, 3144, 347, 355, 333, 339, 351, 329, 3381, 361, 10136, 5003, 379, 365, 373, 7946, 385, 397, 391, 3369, 3371, 3373, 2309, 2325, 2333, 2327, 2331, 2323, 2335, 7178, 7180, 7188, 7190, 7198, 7200, 7208, 7210, 7218, 7220, 2003, 2011, 2289, 2291, 2293, 2295, 2297, 2299, 2301, 2303, 1891, 1893, 1895, 1897, 1899, 1901, 7072, 7062, 7078, 7064, 7084, 7082, 7066, 7068, 1942, 6701, 6703, 7054, 6705, 7056, 7060, 2130, 1985, 1993, 1989, 1978, 5763, 5765, 1913, 5747, 1905, 5739, 1909, 5743, 1907, 1911, 5745, 2955, 5749, 5751, 5753, 5755, 5757, 5759, 5761, 2084, 2034, 2048, 2036, 2217, 2213, 2205, 2209, 2054, 2040, 2080, 2277, 2225, 2255, 2221, 2253, 2219, 2281, 2227, 2223, 2191, 2233, 2092, 2032, 2074, 2030, 2281, 2235, 2064, 2028, 2187, 2185, 2229 };

    this.junkSeedID = new int[] { 5319, 5307, 5305, 5322, 5099, 5310, 5308, 5102, 5101, 5096, 5324, 5306, 5291, 5103, 5292, 5097, 5281, 5098, 5294, 5105, 5106, 5280, 5297, 5311, 5104, 5293, 5318, 5282, 5309 };

    this.ANIMATIONID = 11974;
    this.farmerToBank = new RSTile[] { new RSTile(3081, 3250), new RSTile(3092, 3244) };
    this.farmerTile = new RSTile(3081, 3250);
    this.bankTile = new RSTile(3092, 3244);
    this.status = "";

    this.foodEated = 0;
    this.pickpockets = 0;
    this.failPickpocket = 0;
    this.somethingID = 5295;
    this.startTime = System.currentTimeMillis();
    this.pickpocketing = new int[] { 11942, 378 };
    this.camHeight = new BloddyMasterFarmerStealer.cameraHeight(this);
    this.camRotate = new BloddyMasterFarmerStealer.cameraRotate(this);
    this.guiWait = true;
  }

  public boolean onStart() {
    try {
      URL localURL1 = new URL("http://i48.tinypic.com/313623n.png");
      URL localURL2 = new URL("http://i46.tinypic.com/9prjnt.png");
      this.normal = ImageIO.read(localURL1);
      this.clicked = ImageIO.read(localURL2);
    } catch (MalformedURLException localMalformedURLException) {
      log("Unable to buffer cursor.");
    } catch (IOException localIOException) {
      log("Unable to open cursor image.");
    }
    this.gui = new BloddyMasterFarmerStealer.BloddyMasterFarmerGUI(this);
    this.gui.setVisible(true);
    while (this.guiWait) {
      sleep(100);
    }
    this.startTime = System.currentTimeMillis();
    this.startXP = this.skills.getCurrentExp(17);
    this.startLvl = this.skills.getCurrentLevel(17);
    this.mouse.setSpeed(6);
    BloddyMasterFarmerStealer.cameraRotate.access$002(this.camRotate, true);
    this.camRotate.start();
    BloddyMasterFarmerStealer.cameraHeight.access$102(this.camHeight, true);
    this.camHeight.start();
    log("Antiban initialized!");
    return !this.guiExit;
  }

  public void onFinish() {
    this.mouse.moveOffScreen();
    BloddyMasterFarmerStealer.cameraHeight.access$102(this.camHeight, false);
    BloddyMasterFarmerStealer.cameraRotate.access$002(this.camRotate, false);
  }

  public static void main(String[] paramArrayOfString) {
    new BloddyMasterFarmerStealer();
  }

  private int getCurrentLifepoint() {
    if (this.interfaces.get(748).getComponent(8).isValid()) {
      if (this.interfaces.get(748).getComponent(8).getText() != null) {
        this.HP = Integer.parseInt(this.interfaces.get(748).getComponent(8).getText());
      }
      else
        this.log.severe("Getting lifepoints Error");
    }
    else {
      this.log.warning("HP Interface is not valid");
    }

    return this.HP;
  }

  private int checkEat() {
    int i = getCurrentLifepoint() / 10;
    int j = this.skills.getRealLevel(3);
    if (i <= random(j / 2, j / 1.5D)) {
      if (!this.highLevelMode) {
        this.status = "Eating Food";
        if (this.inventory.contains(this.FOODID)) {
          this.inventory.getItem(new int[] { this.FOODID }).doAction("Eat");
          log("ate food..");
        }
      }
      else if (getMyPlayer().getHPPercent() <= 10) {
        this.log.warning("Low HP! loggin out..");
        this.game.logout(true);
        stopScript(true);
      }
    }

    return 0;
  }

  public int checkInventoryFull() {
    if ((!this.highLevelMode) && 
      (this.bank.isOpen())) if ((this.bank.getCount(new int[] { this.FOODID }) == 0) && (!this.inventory.contains(this.FOODID)) && (this.game.isLoggedIn()))
      {
        log("Out of food! logging out..");
        sleep(random(4000, 5000));
        this.bank.close();
        logOut();
      }

    if (this.inventory.isFull()) {
      if (!this.highLevelMode)
        if (!atBank()) {
          this.status = "Walking to Bank";
          if ((this.walking.getEnergy() != random(60, 100)) || (
            (this.calc.distanceTo(this.walking.getDestination()) < random(5, 12)) || (this.calc.distanceTo(this.walking.getDestination()) > 40)))
          {
            if (!this.walking.walkPathMM(this.farmerToBank)) {
              this.walking.walkTo(this.walking.randomize(this.bankTile, 2, 2));

              return random(1400, 1600);
            }
          }
        } else if (atBank()) {
          openBank();
          bank();
        }
    }
    else if (!this.inventory.contains(this.FOODID)) {
      this.status = "Getting food";
      if (atBank()) {
        openBank();
        bank();
      } else if ((!atBank()) && (
        (this.calc.distanceTo(this.walking.getDestination()) < random(5, 12)) || (this.calc.distanceTo(this.walking.getDestination()) > 40)))
      {
        if (!this.walking.walkPathMM(this.farmerToBank)) {
          this.walking.walkTo(this.walking.randomize(this.bankTile, 2, 2));
          return random(1400, 1600);
        }
      }

    }
    else if (this.inventory.containsOneOf(this.junkSeedID)) {
      try {
        this.inventory.dropAllExcept(this.seedID);
      }
      catch (Exception localException)
      {
      }
    }
    return 0;
  }

  public void checkFail() {
    if (this.addFail == 3) {
      log("out of food! logging out!");
      this.bank.close();
      logOut();
      stopScript();
    }
  }

  private void logOut() {
    this.mouse.move(754, 10, 10, 10);
    this.mouse.click(true);
    this.mouse.move(642, 378, 100, 20);
    this.mouse.click(true);
    sleep(random(2000, 3000));
    stopScript();
  }

  public int loop()
  {
    if (!this.logOut) {
      checkFail();
      checkEat();
      checkInventoryFull();
      if (this.failed) {
        stunned();
      }
      if (!this.inventory.isFull())
        if (!this.highLevelMode) {
          if (this.inventory.contains(this.FOODID)) {
            if (this.calc.distanceTo(this.npcs.getNearest(new String[] { "Master Farmer" })) <= 5) {
              pickPocket();
            } else if (this.calc.distanceTo(this.npcs.getNearest(new String[] { "Master Farmer" })) >= 6)
            {
              this.status = "walking to Farmer";
              this.walking.walkTo(this.walking.randomize(this.npcs.getNearest(new String[] { "Master Farmer" }).getLocation(), 1, 1));

              return random(1500, 1700);
            }
          }
        }
        else if (this.calc.distanceTo(this.npcs.getNearest(new String[] { "Master Farmer" })) <= 5) {
          pickPocket();
        } else if (this.calc.distanceTo(this.npcs.getNearest(new String[] { "Master Farmer" })) >= 6)
        {
          this.status = "walking to Farmer";
          this.walking.walkTo(this.walking.randomize(this.npcs.getNearest(new String[] { "Master Farmer" }).getLocation(), 1, 1));

          return random(1600, 1700);
        }
    }
    else
    {
      logOut();
    }
    return random(100, 200);
  }

  private boolean atBank() {
    return this.calc.distanceTo(this.bankTile) <= 5;
  }

  public int openBank() {
    RSObject localRSObject = this.objects.getNearest(new int[] { this.bBoothID });
    if (!this.bank.isOpen()) {
      if (localRSObject != null) {
        localRSObject.doAction("Use-Quickly");
        sleep(random(200, 300));
      }
      if (localRSObject == null) {
        log("cant find bank :/");
        return random(150, 350);
      }
    }
    return random(150, 350);
  }

  public int bank() {
    this.status = "Banking";
    if (this.bank.isOpen()) {
      if (this.inventory.getCount() >= 1) {
        this.bank.depositAll();
        sleep(random(300, 400));
      }
      if (!this.withdraw10) {
        this.bank.getItem(this.FOODID).doAction("Withdraw-5");
        sleep(random(500, 600));
      } else {
        this.bank.getItem(this.FOODID).doAction("Withdraw-10");
        sleep(random(500, 600));
      }
    }
    return 0;
  }

  public boolean antiBan2() {
    int i = random(1, 45);
    if (i <= 9) {
      if ((i != 1) || 
        (i == 2)) {
        this.mouse.move(random(50, 700), random(50, 450), 2, 2);
        sleep(random(200, 300));
        this.mouse.move(random(50, 700), random(50, 450), 2, 2);
      }
      if ((i != 3) || 
        (i == 4)) {
        this.camera.setAngle(random(1, 360));
        this.mouse.move(random(50, 700), random(50, 450), 2, 2);
      }
      if (i == 6) {
        this.mouse.move(random(50, 700), random(50, 450), 2, 2);
      }
      if (i == 7) {
        this.mouse.move(random(50, 700), random(50, 450), 2, 2);
      }
      if (i == 8) {
        this.camera.setAngle(random(1, 360));
      }
      if (i != 9);
    }
    return true;
  }

  private boolean stunned() {
    int i = random(1, 5);
    this.status = "failed";
    checkEat();
    if (!this.highLevelMode) {
      if (this.doDropSeeds) {
        if (this.inventory.containsOneOf(this.junkSeedID))
          try {
            this.inventory.dropAllExcept(this.seedID);
          }
          catch (Exception localException) {
          }
        else if (!this.inventory.containsOneOf(this.junkSeedID)) {
          antiBan();
        }
        if (i == 2)
          antiBan();
      }
      else {
        antiBan();
      }
      this.failed = false;
    } else {
      antiBan2();
      sleep(random(1400, 1700));
      this.failed = false;
    }
    return true;
  }

  private boolean pickPocket() {
    if (this.failed) {
      stunned();
    } else if ((this.npcs.getNearest(new int[] { this.farmerID }) != null) && (getMyPlayer().getAnimation() != this.ANIMATIONID) && (!getMyPlayer().isInCombat()) && (!getMyPlayer().isMoving()))
    {
      this.status = "pickpocketing";
      doSomethingNPC(this.farmerID, "Pickpocket");
      antiBan2();
      sleep(random(400, 600));
    }
    if (this.failed) {
      stunned();
    }
    return true;
  }

  public boolean doSomethingNPC(int paramInt, String paramString)
  {
    RSNPC localRSNPC = this.npcs.getNearest(new int[] { paramInt });
    if (localRSNPC == null) {
      return antiBan();
    }
    if (localRSNPC.isOnScreen()) {
      localRSNPC.doAction(paramString);
      return true;
    }
    this.walking.walkTo(localRSNPC.getLocation());
    localRSNPC.doAction(paramString);
    return true;
  }

  public boolean antiBan()
  {
    int i = random(1, 16);
    if (i <= 16) {
      if (i == 1) {
        openRandomTab();
        sleep(random(100, 500));
        this.mouse.move(631, 254, 50, 100);
        sleep(random(2200, 2700));
      }
      if (i == 2) {
        this.mouse.move(random(50, 700), random(50, 450), 2, 2);
        sleep(random(200, 400));
        this.mouse.move(random(50, 700), random(50, 450), 2, 2);
      }
      if (i == 3) {
        this.camera.setAngle(random(1, 360));
        this.mouse.move(random(50, 700), random(50, 450), 2, 2);
      }
      if (i == 4) {
        sleep(random(100, 200));
        this.mouse.move(random(50, 700), random(50, 450), 2, 2);
        this.camera.setAngle(random(1, 360));
        this.mouse.move(random(50, 700), random(50, 450), 2, 2);
      }
      if (i == 6) {
        this.camera.setAngle(random(1, 360));
      }
      if (i == 7) {
        this.mouse.move(random(50, 700), random(50, 450), 2, 2);
      }
      if (i == 8) {
        sleep(random(100, 200));
        this.mouse.move(631, 278);
        this.mouse.move(random(50, 700), random(50, 450), 2, 2);
        sleep(random(200, 500));
        if (i == 9) {
          sleep(random(100, 200));
          this.mouse.move(random(50, 700), random(50, 450), 2, 2);
          if (i == 10) {
            this.mouse.move(random(50, 700), random(50, 450), 2, 2);
          }
          if (i == 11) {
            this.camera.setAngle(random(1, 360));
            this.mouse.move(random(50, 700), random(50, 450), 2, 2);
          }
          if (i == 12) {
            this.game.openTab(2);
            sleep(random(1000, 2000));
          }
          if (i == 13) {
            this.mouse.move(random(50, 700), random(50, 450), 2, 2);
            this.camera.setAngle(random(1, 360));
          }
        }
      }
    }

    return true;
  }

  private void openRandomTab() {
    int i = random(1, 14);
    if (i <= 14) {
      if (i == 1) {
        this.game.openTab(2);
      }
      if (i == 2) {
        this.game.openTab(0);
      }
      if (i == 3) {
        this.game.openTab(5);
      }
      if (i == 4) {
        this.game.openTab(9);
      }
      if (i == 6) {
        this.game.openTab(7);
      }
      if (i == 7)
        this.game.openTab(15);
    }
  }

  public void onRepaint(Graphics paramGraphics)
  {
    long l1 = 0L;
    long l2 = 0L;
    long l3 = 0L;
    long l4 = 0L;
    int i = 0;
    int j = 0;
    int k = 0;
    int m = 0;
    int n = 0;
    int i1 = 0;
    l1 = System.currentTimeMillis() - this.startTime;
    l2 = l1 / 1000L;
    if (l2 >= 60L) {
      l3 = l2 / 60L;
      l2 -= l3 * 60L;
    }
    if (l3 >= 60L) {
      l4 = l3 / 60L;
      l3 -= l4 * 60L;
    }

    j = this.skills.getCurrentExp(17);
    k = this.skills.getCurrentLevel(17);
    m = j - this.startXP;
    n = k - this.startLvl;
    i1 = (int)(3600000.0D / l1 * m);
    i = (int)(3600000.0D / l1 * this.pickpockets);

    if (this.game.getCurrentTab() == 4) {
      if (!this.showInventory) {
        paramGraphics.setColor(new Color(0, 0, 0, 190));
        paramGraphics.fillRoundRect(555, 210, 175, 250, 0, 0);

        paramGraphics.setColor(Color.RED);
        paramGraphics.draw3DRect(555, 210, 175, 250, true);

        paramGraphics.setColor(Color.WHITE);
        localObject = new int[] { 225, 240, 255, 270, 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480 };

        paramGraphics.setColor(Color.RED);
        paramGraphics.setFont(new Font("Segoe Print", 1, 14));
        paramGraphics.drawString("Master Farmer Stealer", 561, localObject[0]);
        paramGraphics.drawString("Version: " + this.properties.version(), 561, localObject[1]);

        paramGraphics.setFont(new Font("Lucida Calligraphy", 0, 12));
        paramGraphics.setColor(Color.LIGHT_GRAY);
        paramGraphics.drawString("Run Time: " + l4 + ":" + l3 + ":" + l2, 561, localObject[2]);

        paramGraphics.setColor(Color.RED);
        paramGraphics.drawString(this.pickpockets + " pickpockets", 561, localObject[4]);
        paramGraphics.setColor(Color.LIGHT_GRAY);
        paramGraphics.drawString("pickpockets/hour: " + i, 561, localObject[5]);

        paramGraphics.setColor(Color.RED);
        paramGraphics.drawString("XP Gained: " + m, 561, localObject[6]);
        paramGraphics.setColor(Color.LIGHT_GRAY);
        paramGraphics.drawString("XP/Hour: " + i1, 561, localObject[7]);
        paramGraphics.setColor(Color.RED);
        paramGraphics.drawString("Your level is " + k, 561, localObject[8]);
        paramGraphics.setColor(Color.LIGHT_GRAY);
        paramGraphics.drawString("Lvls Gained: " + n, 561, localObject[9]);
        paramGraphics.setColor(Color.RED);
        paramGraphics.drawString("failed " + this.failPickpocket + " times", 561, localObject[10]);

        paramGraphics.drawString("XP To Next Level: " + this.skills.getExpToNextLevel(17), 561, localObject[12]);

        paramGraphics.setColor(Color.LIGHT_GRAY);
        paramGraphics.drawString("% To Next Level: " + this.skills.getPercentToNextLevel(17), 561, localObject[13]);

        paramGraphics.setColor(Color.RED);
        paramGraphics.drawString("Status: " + this.status, 561, localObject[14]);
        paramGraphics.setColor(Color.LIGHT_GRAY);
        paramGraphics.drawString("By Bloddyharry", 561, localObject[15]);
      }
      paramGraphics.setFont(new Font("Lucida Calligraphy", 0, 12));
      paramGraphics.setColor(new Color(0, 0, 0, 195));
      paramGraphics.fillRoundRect(6, 315, 120, 20, 0, 0);
      paramGraphics.setColor(Color.red);
      paramGraphics.draw3DRect(6, 315, 120, 20, true);
      paramGraphics.setColor(Color.white);
      paramGraphics.drawString("See inventory", 10, 330);

      Object localObject = this.mouse.getLocation();
      if ((((Point)localObject).x >= 6) && (((Point)localObject).x < 126) && (((Point)localObject).y >= 315) && (((Point)localObject).y < 345))
        this.showInventory = true;
      else {
        this.showInventory = false;
      }
    }
    if ((l4 == this.wantedHours) && (l3 == this.wantedMinutes) && (l2 == this.wantedSeconds) && (this.finishAt))
    {
      this.logOut = true;
      if (this.canSay) {
        log(this.wantedHours + " hours " + this.wantedMinutes + " minutes " + this.wantedSeconds + " seconds past, stopping script.");

        this.canSay = false;
      }
    }
    if (this.normal != null) {
      int i2 = this.mouse.getLocation().x;
      int i3 = this.mouse.getLocation().y;
      int i4 = this.mouse.getPressLocation().x;
      int i5 = this.mouse.getPressLocation().y;
      long l5 = System.currentTimeMillis() - this.mouse.getPressTime();
      if ((this.mouse.getPressTime() == -1L) || (l5 >= 1000L)) {
        paramGraphics.drawImage(this.normal, i2 - 8, i3 - 8, null);
      }
      if (l5 < 1000L) {
        paramGraphics.drawImage(this.clicked, i4 - 8, i5 - 8, null);
        paramGraphics.drawImage(this.normal, i2 - 8, i3 - 8, null);
      }
    }
  }

  public void messageReceived(MessageEvent paramMessageEvent) {
    if (paramMessageEvent.getID() != 109)
      return;
    String str = paramMessageEvent.getMessage();
    if (str.contains("You pick the")) {
      this.pickpockets += 1;
    }
    if (str.contains("You can't do that")) {
      this.walking.walkTileMM(this.bankTile);
      sleep(random(1500, 2000));
    }
    if (str.contains("You fail")) {
      this.failPickpocket += 1;
      this.failed = true;
    }
    if (str.contains("You eat")) {
      this.foodEated += 1;
    }
    if (str.contains("You've just advanced")) {
      log("Congrats on level up, Screenshot taken!");
      sleep(random(1500, 2500));
      if (this.interfaces.canContinue()) {
        this.interfaces.clickContinue();
      }
      this.gainedLvls += 1;
      if ((this.skills.getCurrentLevel(17) == this.wantedLevel) && (this.finishWhenLevel))
      {
        this.logOut = true;
        if (this.canSay) {
          log("Reached level " + this.skills.getCurrentLevel(17) + " in thieving, stopping script");

          this.canSay = false;
        }
      }
    }
  }
}